﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using SailHero.Soft.FrameWork.DAO.Core;
using SailHero.Soft.FrameWork.IDomainRepositories.Contracts;
using SailHero.Soft.FrameWork.Models;

namespace SailHero.Soft.FrameWork.DomainRepositories
{
    public class ModelRepository : EFRepository<SYS_MODEL>, IModelRepository
    {
        public ModelRepository()
            : base("AUTHCODEDBORA")
        {
        }

        public List<SYS_MODEL> GetListByUserPermission(string UserIds)
        {
            #region 对应的sql
//            string sql = @"select *
//                          FROM ""Sys_Model""
//                         WHERE ""ModelID"" in (select distinct ""ModelID""
//                                             From ""Sys_RolePermission"" p
//                                            inner join ""Sys_UserInRole"" ur
//                                               on p.""RoleID"" = ur.""RoleID""
//                                              and ur.""UserID"" = '{0}')
//                         Order by ""OrderNum"" "; 
            #endregion

            var source0 = from c in DbContext.Set<SYS_MODEL>()
                          where
                              (from temp1 in DbContext.Set<SYS_USERINROLE>()
                               join temp2 in DbContext.Set<SYS_ROLEPERMISSION>() on temp1.ROLEID equals temp2.ROLEID into deval
                               from x in deval.DefaultIfEmpty()
                               where temp1.USERID == UserIds
                               select x.MODELID
                                   ).Distinct().Contains(c.MODELID)
                          select c;
            return source0.ToList();
        }
    }
}